﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;
using Bitis_KeToanThue_Pro_DungChung.Common;
using Bitis_KeToanThue_Pro_QuanTri.AccessDatabase;

namespace Bitis_KeToanThue_Pro_QuanTri.Form
{
    public partial class frmDanhMucPhongBan : Telerik.WinControls.UI.RadForm
    {
        DanhMucPhongBan dm;
        public static string mapbcu = "";
        public frmDanhMucPhongBan()
        {
            InitializeComponent();
            dm = new DanhMucPhongBan();
            khoitaogrid();
            loadluoi();
            if (BienHeThong.ht_quyencapnhat == 1)
            {
                btnSua.Enabled = true;
                btnThem.Enabled = true;
            }
            else
            {
                btnSua.Enabled = false;
                btnThem.Enabled = false;
            }
            if (BienHeThong.ht_quyenxoa == 1)
            {
                btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }
        public void loadluoi()
        {
            this.radGridView1.DataSource = dm.loadgird();
        }
        public void khoitaogrid()
        {
            this.radGridView1.EnableHotTracking = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.EnableFiltering = true;
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
            this.radGridView1.MasterTemplate.ShowFilteringRow = false;
            this.radGridView1.MasterTemplate.AutoExpandGroups = true;
            //this.radGridView1.TableElement.RowHeight = 50;
            this.radGridView1.MasterTemplate.AllowAddNewRow = false;
            this.radGridView1.MasterTemplate.AutoGenerateColumns = false;
            this.radGridView1.MasterTemplate.AllowEditRow = true;
            this.radGridView1.MasterTemplate.AllowDeleteRow = false;
            this.radGridView1.MasterTemplate.EnableSorting = true;
            this.radGridView1.MasterTemplate.AllowAutoSizeColumns = true;
            this.radGridView1.AutoScroll = true;
            this.radGridView1.EnableAlternatingRowColor = true;


            GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn() { HeaderText = "", Name = "Chon", Width = 20, IsVisible = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(checkBoxColumn);

            GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn() { FieldName = "MaPB", Name = "MaPB", HeaderText = "Mã Phòng Ban", Width = 130, IsVisible = true, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn);

            GridViewTextBoxColumn textBoxColumn1 = new GridViewTextBoxColumn() { FieldName = "TenPB", Name = "TenPB", HeaderText = "Phòng Ban", Width = 280, IsVisible = true, ReadOnly = true, TextAlignment = ContentAlignment.MiddleCenter };
            this.radGridView1.Columns.Add(textBoxColumn1);






        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public void soanlai()
        {
            txtMaPB.Text = "";
            txtPB.Text = "";
            txtTimKiem.Text = "";

        }
        private void btnSoanLai_Click(object sender, EventArgs e)
        {
            soanlai();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaPB.Text) && !string.IsNullOrEmpty(txtMaPB.Text))
            {
                if (dm.sua(mapbcu, txtPB.Text, txtMaPB.Text) == 1)
                {
                    RadMessageBox.Show("Cập Nhật Phòng Ban : " + txtMaPB.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                    soanlai();
                    loadluoi();
                }
                else
                {
                    RadMessageBox.Show(String.Format("Phòng Ban : {0} đã tồn tại", txtMaPB.Text), "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                }
            }
            else
            {
                RadMessageBox.Show("Bạn chưa nhập thông tin Phòng ban", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            DialogResult kq = RadMessageBox.Show("Bạn có muốn chắc xóa các phòng ban : được chọn không ???", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
            if (kq == DialogResult.Yes)
            {
                for (int i = 0; i < radGridView1.Rows.Count; i++)
                {
                    if (radGridView1.Rows[i].Cells[0].Value != null)
                    {
                        bool isChecked = (bool)radGridView1.Rows[i].Cells[0].Value;
                        if (isChecked == true)
                        {
                            dm.xoa(ClassXuLyChung.GetSafeString(radGridView1.Rows[i].Cells[1].Value));
                        }
                    }
                }
                RadMessageBox.Show("Xóa Thành Công", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                soanlai();
                loadluoi();
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaPB.Text) && !string.IsNullOrEmpty(txtPB.Text))
            {
                if (dm.them(txtMaPB.Text, txtPB.Text) == 1)
                {
                    RadMessageBox.Show("Thêm Thành Công Phòng Ban : " + txtMaPB.Text, "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Info);
                    soanlai();
                    loadluoi();

                }
                else
                {
                    RadMessageBox.Show(String.Format("Phòng Ban: {0} đã tồn tại", txtMaPB.Text), "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                }
            }
            else
            {
                RadMessageBox.Show("Bạn chưa nhập thông tin phòng ban", "Thông Tin", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
            }

        }

        private void radGridView1_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {

            UpdatePanelInfo(this.radGridView1.CurrentRow);

        }
        private void UpdatePanelInfo(GridViewRowInfo currentRow)
        {
            if (currentRow != null && !(currentRow is GridViewNewRowInfo))
            {
                mapbcu = ClassXuLyChung.GetSafeString(currentRow.Cells["MaPB"].Value);
                txtMaPB.Text = ClassXuLyChung.GetSafeString(currentRow.Cells["MaPB"].Value);


                txtPB.Text = ClassXuLyChung.GetSafeString(currentRow.Cells["TenPB"].Value);


            }
            else
            {
                soanlai();
                mapbcu = "";
            }
        }      

        private void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
         
            ClassXuLyChung.tuychontimkiemtrenluoi(txtTimKiem, radGridView1, 1, 0);
        }

        private void radGridView1_Click(object sender, EventArgs e)
        {

        }

    }
}
